package com.ym9527.controller;

import com.ym9527.controller.apiEntity.*;
import com.ym9527.entity.Course;
import com.ym9527.entity.CourseStudent;
import com.ym9527.entity.PunchCardRecord;
import com.ym9527.entity.User;
import com.ym9527.jpa.CourseJpa;
import com.ym9527.jpa.CourseStudentJpa;
import com.ym9527.jpa.PunchCardRecordJpa;
import com.ym9527.jpa.UserJpa;
import com.ym9527.utils.R;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("teacher")
public class TeacherRelevantController {


    @Resource
    CourseJpa courseJpa;

    @Resource
    CourseStudentJpa courseStudentJpa;

    @Resource
    PunchCardRecordJpa punchCardRecordJpa;

    @Resource
    UserJpa userJpa;

    /**
     * 这是添加课程接口
     * @return
     */
    @PostMapping("addCourse")
    @Transactional()
    public R addCourse(HttpServletRequest request,@RequestBody Course addCourseAE){


        String studentName=request.getHeader("user").split("-")[0];

        User user=userJpa.findByUserName(studentName);

        Course addCourseAE1=new Course();




        addCourseAE1.setCourseName(addCourseAE.getCourseName());

        addCourseAE1.setLatitude(addCourseAE.getLatitude());
        addCourseAE1.setLongitude(addCourseAE.getLongitude());

        addCourseAE1.setPunchCardScope(addCourseAE.getPunchCardScope());

        addCourseAE1.setCreateTeacherId(user.getUserId());

        addCourseAE1.setPunchCardWeek(addCourseAE.getPunchCardWeek());
        addCourseAE1.setPunchCardDate(addCourseAE.getPunchCardDate());

        courseJpa.save(addCourseAE1);

        return R.ok();
    }


    /**
     * 这是获取全部学生
     * @return
     */
    @GetMapping("getAllStudent")
    @Transactional()
    public R GetAllStudent(){

        List<User> users=this.userJpa.findByProfession("student");

        return R.ok(users);
    }

    /**
     * 这是修改课程接口
     * @return
     */
    @PostMapping("updateCourse")
    @Transactional()
    public R UpdateCourse(@RequestBody AddCourseAE addCourseAE){

        Course course=courseJpa.findById(addCourseAE.getCourseId()).orElse(null);

        course.setLatitude(addCourseAE.getLatitude());
        course.setLongitude(addCourseAE.getLongitude());

        courseJpa.save(course);

        return R.ok("修改成功");
    }

    /**
     * 这是删除课程接口
     * @return
     */
    @DeleteMapping("deleteCourse")
    @Transactional()
    public R DeleteCourse(@RequestBody AddCourseAE addCourseAE){

        Course course=courseJpa.findById(addCourseAE.getCourseId()).orElse(null);

        List<CourseStudent> courseStudentList=this.courseStudentJpa.findByCourseId(addCourseAE.getCourseId());

        courseStudentList.forEach(item->{
            courseStudentJpa.delete(item);
        });
        courseJpa.delete(course);
        return R.ok("删除成功!");
    }


    @DeleteMapping("deleteStudent")
    @Transactional()
    public R DeleteStudent(@RequestBody DeleteStudentAE deleteStudentAE){

        User user=userJpa.findByUserName(deleteStudentAE.getStudentName());


        if(null!=user){

            List<CourseStudent> list=this.courseStudentJpa.findByStudentId(user.getUserId());
            list.forEach(item->{

                courseStudentJpa.delete(item);

            });
            List<PunchCardRecord> punchCardRecordList=punchCardRecordJpa.findByPunchCardStudentId(user.getUserId());

            punchCardRecordList.forEach(item->{

                punchCardRecordJpa.delete(item);
            });

            userJpa.delete(user);

            return R.ok("删除成功!");

        }else {

            return R.error("删除失败学生不存在!");

        }




    }

    /**
     * 这是获取全部课程的接口
     *
     * @return
     */
    @PostMapping("getAllCourse")
    @Transactional()
    public R GetAllCourse(HttpServletRequest request){

        String studentName=request.getHeader("user").split("-")[0];

        User user=userJpa.findByUserName(studentName);

        List<Map> courseList=new LinkedList<>();

        List<Course> courseList1=this.courseJpa.findByCreateTeacherId(user.getUserId());

        courseList1.forEach(item->{

            String dateString=new String();
            SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd");
            dateString=f.format(new Date());
            /**
             * 这里开始装载
             */
            Map<String,Object> hashMap=new HashMap();

            /**
             * 这里装载课程打卡人数
             */
            List<PunchCardRecord> punchCardRecordList=punchCardRecordJpa.findByPunchCardCourseIdAndDateString(item.getCourseId(),dateString);
            List<User> punchCardStudent=new ArrayList<>();
            punchCardRecordList.forEach(item2->{
                User user1=userJpa.getById(item2.getPunchCardStudentId());
                punchCardStudent.add(user1);
            });
            hashMap.put("punchCardStudentInfo",punchCardStudent);

            /**
             * 这里装载课程预计打卡人数
             */
            List<CourseStudent> courseStudentList=courseStudentJpa.findByCourseId(item.getCourseId());
            List<User> planPunchCardStudent=new LinkedList<>();
            courseStudentList.forEach(item3->{
                User user2=userJpa.getById(item3.getStudentId());
                planPunchCardStudent.add(user2);
            });
            hashMap.put("punchCardStudentPlanInfo",planPunchCardStudent);

            /**
             * 这里装载课程信息
             */
            hashMap.put("courseInfo",item);

            courseList.add(hashMap);
        });


        return R.ok(courseList);
    }


    /**
     * 这是获取单个课程的学生接口
     * @return
     */
    @PostMapping("getCourseStudent")
    @Transactional()
    public R GetCourse(@RequestBody GetCourseStudentAE getCourseStudentAE ){

        List<User> users=new LinkedList<>();

        List<CourseStudent> courseStudentList=this.courseStudentJpa.findByCourseId(getCourseStudentAE.getCourseId());

        courseStudentList.forEach(item->{

            User user=this.userJpa.getById(item.getStudentId());
            users.add(user);
        });

        return R.ok(users);
    }

    /**
     * 这是添加课程学生接口
     * @return
     */
    @PostMapping("addCourseStudent")
    @Transactional()
    public R AddCourseStudent(@RequestBody AddTeacherStudentAE addTeacherStudentAE){

        User user=this.userJpa.findByUserName(addTeacherStudentAE.getStudentName());

        CourseStudent courseStudent=courseStudentJpa.findByCourseIdAndStudentId(addTeacherStudentAE.getCourseId(),user.getUserId());

        if(null==courseStudent){
            CourseStudent courseStudent2=new CourseStudent();

            courseStudent2.setStudentId(user.getUserId());
            courseStudent2.setCourseId(addTeacherStudentAE.getCourseId());

            courseStudentJpa.save(courseStudent2);
        }else{
            return R.error("请勿重复添加");
        }


        return R.ok("添加成功");
    }

    /**
     * 这是添加学生接口
     * @return
     */
    @PostMapping("addStudent")
    @Transactional()
    public R AddStudent(@RequestBody AddStudentAE addStudentAE){

        User user=userJpa.findByUserName(addStudentAE.getStudentName());

        if(null==user){

            User user1=new User();
            user1.setUserName(addStudentAE.getStudentName());
            user1.setPassword(addStudentAE.getPassWord());
            user1.setProfession("student");
            user1.setStatus("正常");

            userJpa.save(user1);

        }else{
            return R.error("请勿重复添加");
        }

        return R.ok("添加成功");
    }

}
